package com.fbli.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class shanfatest {
    public static void main(String[] args) {
        boolean b = isValid("()");
        System.out.println(b);
    }

    public static boolean isValid(String s){
        //空字符串返回true
        if (s==null&&s.length()>0){
            return true;
        }
        Stack<Character> stack=new Stack<>();
        char[] chars=s.toCharArray();//将字符串转为字符数组
        Map<Character,Character> map=new HashMap<>();
        map.put(')','(');
        map.put(']','[');
        map.put('}','{');//将括号对作为键值对存入hashMap中
        //遍历字符串数组
        for (int i = 0; i < chars.length; i++) {
            if (map.containsValue(chars[i])){
                //如果是左括号，将其压入栈中
                stack.push(chars[i]);
            }
            //&& (stack.isEmpty() || !stack.pop().equals(chars[i]))
            //如果是右括号，且栈为空或者栈顶元素于该字符不匹配，则返回false
            if (map.containsKey(chars[i])){
                if ((stack.isEmpty() || !stack.pop().equals(chars[i]))){
                    return false;
                }
            }else if(stack.empty()){
                return false;
            }
        }
        //出栈后中元素不为空，返回false
        if (!stack.empty()){
            return false;
        }
        //其余情况返回ture,匹配
        return true;
    }




    public static int GetCoinCount(int N){
        // write code here
        int temp=1024-N;
        int result=0;
        if(temp>=1&&temp<4){
            return temp;
        }else if(temp>=4&&temp<16){
            result=temp/4+temp/4%4;
            return result;
        }else if(temp>=16&&temp<64){
            result=temp/16+temp%16/4+temp%16%4;
            return result;
        }else{
            result=temp/64+temp%64/16+temp%64%16/4+temp%64%16%4;
            return result;
        }
    }

}
